package com.beta.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.beta.entity.Assets;
import com.beta.mapper.AssetsMapper;
import com.beta.service.IAssetsService;
import com.beta.service.IDateTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 *
 * Assets 表数据服务层接口实现类
 *
 */
@Service
public class AssetsService extends SuperServiceImpl<AssetsMapper, Assets> implements IAssetsService {

    @Autowired
    private IDateTimeService dateTimeService;

    @Override
    public Assets getProductItemPosterImage(Integer productItemId) {
        if (Objects.isNull(productItemId)) {
            return null;
        }

        Assets whereEntity = new Assets()
                .setViewableId(productItemId)
                .setType("Image")
                .setViewableType("ProductItem");
        return baseMapper.selectOne(whereEntity);
    }

    @Override
    public Assets createProductItemPosterImage(Integer productItemId, String fileName) {
        Date dbDate = dateTimeService.toDbDate();
        Assets newAssets = new Assets()
                .setViewableId(productItemId)
                .setViewableType("ProductItem")
                .setAttachment(fileName)
                .setType("Image")
                .setCreatedAt(dbDate)
                .setUpdatedAt(dbDate);
        baseMapper.insertSelective(newAssets);
        return selectOne(new Assets().setViewableId(productItemId).setViewableType("ProductItem").setType("Image"));
    }

    @Override
    public void deleteProductItemPosterImage(Integer productItemId) {
        Assets deleteWhereEntity = new Assets()
                .setViewableId(productItemId)
                .setType("Image")
                .setViewableType("ProductItem");
        baseMapper.deleteSelective(deleteWhereEntity);
    }
}